import 'package:honeypotflutter/modules/package/model/package_list_model.dart';
import 'package:honeypotflutter/net/public_http.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_modal.dart';
import '../model/package_param.dart';
import '../model/package_renewal_Info.dart';

class PackageApi {
  /*
  * 列表
  * */
  static Future<List<PackageListItemModel>> packageList(PackageParam param) async {
    try {
      Map<String, dynamic> newParam = {
        "IsTimeout": param.IsTimeout,
        "doorState": param.doorState,
        "page": param.page,
        "pageSize": param.pageSize,
        "keyword": param.keyword,
        "deviceId": param.deviceId,
      };
      Map<String, dynamic> result = await MGHttp.post(
        "api/services/app/BusinessPackage/CourierSearchPackageListAsync",
        params: newParam,
      );
      PackageListModel listModel = PackageListModel.fromJson(result);
      return listModel.items ?? [];
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 撤销
  * */
  static Future<bool> recallPackage(String orderId) async {
    try {
      Map<String, String> param = {"BusinessOrderId": orderId};
      await MGHttp.post("api/services/app/BusinessPackage/RecallPackageAsync", params: param);
      return true;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 重发短信
  * */
  static Future<bool> resend(String orderId) async {
    try {
      Map<String, String> param = {"BusinessOrderId": orderId};
      await MGHttp.post(
        "api/services/app/BusinessPackage/RepeatSendMessageAsync",
        params: param,
        progress: "短信发送中...",
      );
      return true;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 修改手机号
  * */
  static Future<bool> modifyPhone(String id, String orderPhone) async {
    try {
      Map<String, String> param = {"id": id, "orderPhone": orderPhone};
      await MGHttp.post("api/services/app/BusinessPackage/ModifyPhoneAsync", params: param);
      return true;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 续费信息
  * */
  static Future<PackageRenewalInfo> getRenewalInfo(int deviceId) async {
    try {
      Map<String, dynamic> result = await MGHttp.post("api/services/app/BusinessPackage/ModifyPhoneAsync?deviceId=${deviceId}");
      PackageRenewalInfo model = PackageRenewalInfo.fromJson(result);
      return model;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 续费
  * */
  static Future<String> quickRenewal(int deviceId) async {
    try {
      String result = await MGHttp.post(
        "api/services/app/BusinessPackage/CourierQuickRenewal?deviceId=${deviceId}",
        progress: "续费中...",
      );
      return result;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }
}
